package lab.poc.erl;

/**
 * Object utilities.
 */
final class Objects {

	private Objects() {
		throw new AssertionError();
	}

	/**
	 * {@code null}-aware equals.
	 */
	public static boolean equal(final Object o1, final Object o2) {
		if (o1 == o2) {
			return true;
		}
		if (o1 != null) {
			return o1.equals(o2);
		}
		return false;
	}

	/**
	 * Returns the first non-null value of its arguments.
	 */
	public static <T> T coalesce(final T fst, final T snd) {
		return (fst != null) ? fst : snd;
	}

	/**
	 * Returns the first non-null value of its arguments.
	 */
	public static <T> T coalesce(final T fst, final T snd, final T thd) {
		return (fst != null) ? fst : (snd != null) ? snd : thd;
	}

	/**
	 * Returns the first non-null value of its arguments.
	 */
	public static <T> T coalesce(final T... args) {
		for (final T arg : args) {
			if (arg != null) {
				return arg;
			}
		}
		return null;
	}

	public static int hash(final Object o) {
		if (o == null) {
			return 0;
		}
		return o.hashCode();
	}

	public static int hashCombine(int seed, final int hash) {
		seed ^= hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
		return seed;
	}
}
